#!/usr/bin/env python
import sys
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from zhineng_conn import *
import sys
import re
import os
sys.setdefaultencoding('utf-8')


def get_citys(city_id,conn):
    if city_id == 0 or city_id == '-' :
        return None

    query = 'select fullpath from t_displocal where displocalid=%s' % city_id
    #print query
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None
    
    query = "select displocalid, localname from t_displocal where displocalid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)

def get_city_list(conn):

    city_dict = {}
    query = 'select displocalid, localname from t_displocal where pid=0'
    #print query
    items = conn.get_results(query)
    for item in items:
        city_id = item[0]
        city_name = item[1]
        
        if not city_dict.has_key(str(city_id)):
            city_dict[str(city_id)] = city_name
    
    
    return city_dict

def get_cates(cate_id,conn):
    if cate_id == 0  or cate_id == '-' :
        return None

    query = 'select fullpath from t_dispcategory where dispcategoryid=%s' % cate_id
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None
    
    query = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)
    

def get_thirth_cat(cate_id ,param,conn):    
    #print param
    param_id = 0
    query = 'select param_id from t_cate_param where cate_id = %s;' % cate_id
    items = conn.get_results(query)
    if len(items) > 0:
        param_id = items[0][0]
        
    #print param_id
    if param_id == 0:
        return get_second_cat(cate_id ,conn)
    
    reg = '{"I":%s,"V":"(\w*)"}'%param_id
    #print reg
    pattern =  re.compile(reg)
    matchs = pattern.search(param)
    #print reg
    objval = "0"
    #print matchs
    try:
        objval = matchs.group(1)
    except Exception,e:
        return get_second_cat(cate_id,conn )
        
    filter_str = "objecttype=%s"%objval
    #print filter_str
    fullpath = ''
    query = "select fullpath from t_dispcategory where cateid =%s and filter='%s'" % (cate_id,filter_str)
    items = conn.get_results(query)
    if len(items) > 0 :
        fullpath = items[0][0]
    #print fullpath
    if fullpath == '':
        return get_second_cat(cate_id ,conn)
    
    query = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query )

def get_second_cat(cate_id,conn ):
    
    fullpath = ''
    query = 'select fullpath from t_dispcategory where cateid =%s and depth=1;' % (cate_id)
    items = conn.get_results(query)
    if len(items) > 0 :
        fullpath = items[0][0]
    #print fullpath
    if fullpath == '':
        query = "select cateid, catename from t_category where cateid=%s" % cate_id
        return conn.get_results(query)
        
    query = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    return conn.get_results(query )


def get_gs_citys(city_id,conn):
    if city_id == 0 :
        return None

    query = 'select fullpath from t_local where localid=%s' % city_id
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None
    
    query = "select localid, localname from t_local where localid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)
    
def get_gs_cates(cate_id,conn):
    if cate_id == 0 :
        return None

    query = 'select fullpath from t_category where cateid=%s' % cate_id
    items = conn.get_results(query)
    fullpath = None
    if len(items)>0:
        fullpath = items[0][0]
    
    if fullpath is None:
        return None
    
    query = "select cateid, catename from t_category where cateid in (%s) order by depth asc" % fullpath
    #print q
    return conn.get_results(query)